<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<!doctype html>

<html class="no-js">
<style type="text/css">
.am-radius {
	color: gray;
	padding-right: 1%;
}
</style>
<head>
<%@ include file="./commoms/link.txt"%>
<script src="assets/js/pagejs/EEIndex.js"></script>
</head>
<body>
	<%@ include file="./commoms/userHeader.txt"%>
	<div class="admin-content">
		<div class="admin-content-body">
			<div class="am-container" style="margin-top: 40px;">
			

				<div id="pagecontainer"></div>
	   <div class="am-panel am-panel-default "  >
				<div class="am-panel-hd"  >
					<h2 class="am-panel-title ">
						<strong>位置信息</strong>
					</h2>
				</div>

				<div class="am-panel-bd">
					
						<div id="container" style="height: 400px; border: 1px solid gray"
							></div>
			<div class="am-alert am-alert-secondary" data-am-alert>
 路线规划
</div>
<div id="driving_way">
		<select>
			<option value="0">最少时间</option>
			<option value="1">最短距离</option>
			<option value="2">避开高速</option>
		</select>
		<input type="text" id="device"  />
		<input type="button" id="result" value="查询"/>
	</div>
	<div id="r-result"></div>
				</div>

			</div>

			</div>


		</div>
	
	</div>
	</div>
<script src="assets/js/pagejs/EEIndex.js"></script>
<script type="text/javascript">

var myGeo = new BMap.Geocoder();
var map = new BMap.Map("container"); // 创建地图实例

	var longitude=116.331398;
	 var latitude=39.897445;

var point = new BMap.Point(longitude,latitude );
map.centerAndZoom(point, 12);
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.addControl(new BMap.MapTypeControl());
map.addControl(new BMap.GeolocationControl());
var geolocation = new BMap.Geolocation();
var ldevices =null;
var lchildpos=null;
var geoc = new BMap.Geocoder();  
var mychildposition=[];
var output = "从从您现在的位置到孩子位置需要";


getall();
    
setInterval(getall, 10000);

function getall(){
	map.clearOverlays(); 
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
			
			 var convertor = new BMap.Convertor();
		        var pointArr = [];
		        pointArr.push(r.point);
		        convertor.translate(pointArr, 1, 5, function (data){
		       	 if(data.status === 0) {
		       		 var point=data.points[0];
		    	        var marker = new BMap.Marker(point);
		    	        map.addOverlay(marker);
		    	        var label = new BMap.Label("您的位置",{offset:new BMap.Size(20,-10)});
		    	        marker.setLabel(label); //添加百度label
		    	        map.setCenter(point);
		              	map.panTo(point);
		              	myposition=point;
		              	marker.addEventListener("click", function(){    
		              		 getGeoAddress(point)  
		              		});
		    	      }
		       	 });
		}
		else {
			alert('failed'+this.getStatus());
		}        
	},{enableHighAccuracy: true})

	
	myAjax(domain + "/api/device/getlastmessage.do", {}, function(data) {

		if (data.code == 200) {
        if(data.result!=null)
        	{
        	 ldevices=data.result;
        	 
        	 var convertor = new BMap.Convertor();
 	        var pointArr = [];
 	        $.each(ldevices,function(i, item) {
        	        		var message=item.message;
        	        		if(message!=null&&typeof(message) != "undefined")
        	        			{
      	mychildposition[i]=new BMap.Point(message.longtitude,message.latitude);
   
      	pointArr.push(mychildposition[i]);
        	        			}
            	});
        		
	        convertor.translate(pointArr, 1, 5, function (data){
	       	 if(data.status === 0) {
	       		 
	       	   for (var i = 0; i < data.points.length; i++) {
	               var point=data.points[i];
	               mychildposition[i]=point;
	    	        var marker = new BMap.Marker(point);
	    	      
	    	        map.addOverlay(marker);
	    	        var label = new BMap.Label(ldevices[i].holder+"的位置",{offset:new BMap.Size(20,-10)});
	    	        marker.setLabel(label); //添加百度label
	    	        
	              	marker.addEventListener("click", function(){    
	              		 getGeoAddress(point)  
	              		});
	    	      }
	           }
	       		
	       	 });
        
        	
        	
        	}
		}
	});
	
}
var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
	$("#result").click(function(){
		map.clearOverlays(); 
		var i=$("#driving_way select").val();
		var start=myposition,end=null;
		var holder=$('#device').val();
		 for (var i = 0; i < ldevices.length; i++) {
             if(ldevices[i].holder==holder)
            	 {
            	
            	 end=mychildposition[i];
            	 }
  	      
         }
		 if(end==null)
			 {
			 
			 myAlert("提示","持有者姓名不正确",function(){
				 
				
			 });
			 return;
			 }
	    
		search(start,end,routePolicy[i]); 
		function search(start,end,route){ 
			var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true,panel: "r-result"},policy: route
				,onSearchComplete: function (results){
					if (driving.getStatus() != BMAP_STATUS_SUCCESS){
						return ;
					}
					var plan = results.getPlan(0);
					output='';
					output += plan.getDuration(true) + "\n";                //获取时间
					output += "总路程为：" ;
					output += plan.getDistance(true) + "\n";             //获取距离
				},
				onPolylinesSet: function(){        
					setTimeout(function(){alert(output)},"2000");
			}});
			driving.search(start,end);
		}
	});
	function getGeoAddress(point) {
		myGeo.getLocation(point,
				function(result) {
					if (result) {
						geoaddress = result.address;
						lat = point.lat;
						lng = point.lng;
						infoWindow = new BMap.InfoWindow(geoaddress, opts); // 创建信息窗口对象
						map.openInfoWindow(infoWindow,point);
					}
				});

	}
	var opts = {
			width : 250, // 信息窗口宽度
			height : 100, // 信息窗口高度
			title : "地址" // 信息窗口标题
		}
	

</script>

</body>

<a href="#"
	class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu"
	data-am-offcanvas="{target: '#admin-offcanvas'}"></a>
</html>
